#import "tool_ui.h"

void uiClearAnimationObject(UIAnimationObject *Obj)
{
	Obj->path_table.clear();
	Obj->delay = 0.0f;
	Obj->total_time = 0.0f;
}

void uiLoadColorFromXML(mxml_node_t *Element, ccColor3B *Buf)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "r"))
		{
			Buf->r = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "g"))
		{
			Buf->g = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "b"))
		{
			Buf->b = tmxmlElementReadChildInt(_Bouy);
		}
	}
}

void uiLoadPointFromXML(mxml_node_t *Element, CGPoint *Buf)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "x"))
		{
			Buf->x = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "y"))
		{
			Buf->y = tmxmlElementReadChildInt(_Bouy);
		}
	}
}

void uiLoadSizeFromXML(mxml_node_t *Element, CGSize *Buf)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "width"))
		{
			Buf->width = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "height"))
		{
			Buf->height = tmxmlElementReadChildInt(_Bouy);
		}
	}
}

void uiLoadCenterRectFromXML(mxml_node_t *Element, UICenterRect *Buf)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "point"))
		{
			uiLoadPointFromXML(_Bouy, &Buf->center);
		}
		else if (tmxmlIsElement(_Bouy, "size"))
		{
			uiLoadSizeFromXML(_Bouy, &Buf->size);
		}
	}
}

void uiLoadImageObjectFromXML(mxml_node_t *Element, UIImageObject *Object)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "x"))
		{
			Object->position.x = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "y"))
		{
			Object->position.y = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "path"))
		{
			tmxmlElementReadChildText(_Bouy, Object->path);
		}
	}
}

void uiLoadAnimationObjectFromXML(mxml_node_t *Element, UIAnimationObject *Object)
{
	mxml_node_t *_Bouy, *_SubBouy;
	char _Buf[STRING_BUF_SIZE];

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "image"))
		{
			_SubBouy = mxmlGetFirstChild(_Bouy);
			for (; NULL != _SubBouy; _SubBouy = mxmlWalkNext(_SubBouy, _Bouy, MXML_NO_DESCEND))
			{
				if (tmxmlIsElement(_SubBouy, "path"))
				{
					memset(_Buf, 0, sizeof _Buf);
					tmxmlElementReadChildText(_SubBouy, _Buf);
					Object->path_table.push_back(std::string(_Buf));
				}
			}
		}
		else if (tmxmlIsElement(_Bouy, "delay"))
		{
			Object->delay = tmxmlElementReadChildFloat(_Bouy);
		}
	}
	Object->total_time = Object->delay * Object->path_table.size();
}

void uiLoadMenuItemImageObjectFromXML(mxml_node_t *Element, UIMenuItemImageObject *Object)
{
	mxml_node_t *_Bouy;

	strcpy(Object->path_normal, "NULL");
	strcpy(Object->path_selected, "NULL");
	strcpy(Object->path_disabled, "NULL");
	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "x"))
		{
			Object->position.x = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "y"))
		{
			Object->position.y = tmxmlElementReadChildInt(_Bouy);
		}
		else if (tmxmlIsElement(_Bouy, "path_normal"))
		{
			tmxmlElementReadChildText(_Bouy, Object->path_normal);
		}
		else if (tmxmlIsElement(_Bouy, "path_selected"))
		{
			tmxmlElementReadChildText(_Bouy, Object->path_selected);
		}
		else if (tmxmlIsElement(_Bouy, "path_disabled"))
		{
			tmxmlElementReadChildText(_Bouy, Object->path_disabled);
		}
	}
}

void uiLoadMenuItemAniObjectFromXML(mxml_node_t *Element, UIMenuItemAniObject *Object)
{
	mxml_node_t *_Bouy;

	_Bouy = mxmlGetFirstChild(Element);
	for (; NULL != _Bouy; _Bouy = mxmlWalkNext(_Bouy, Element, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_Bouy, "position"))
		{
			uiLoadPointFromXML(_Bouy, &Object->position);
		}
		else if (tmxmlIsElement(_Bouy, "path"))
		{
			tmxmlElementReadChildText(_Bouy, Object->path);
		}
		else if (tmxmlIsElement(_Bouy, "ani"))
		{
			uiLoadAnimationObjectFromXML(_Bouy, &Object->ani);
		}
	}
}

CCSprite *uiCreateImageObject(UIImageObject const *Object, CCNode *Node)
{
	CCSprite *_Ret;

	_Ret = [[CCSprite alloc] initWithSpriteFrameName:[NSString stringWithUTF8String:Object->path]];
	[Node addChild:_Ret];
	[_Ret release];
	[_Ret setPositionInPixels:Object->position];
	return _Ret;
}

CCAnimation *uiCreateAnimationObject(UIAnimationObject const *Object)
{
	int i;
	CCAnimation *_Ret;
	NSString *_String;
	NSMutableArray *_Array;

	if (0 < Object->path_table.size())
	{
		_Array = [NSMutableArray array];
		for (i = 0; i < Object->path_table.size(); i++)
		{
			_String = [NSString stringWithUTF8String:Object->path_table[i].c_str()];
			[_Array addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_String]];
		}
		_Ret = [CCAnimation animationWithFrames:_Array delay:Object->delay];
		return _Ret;
	}
	else
	{
		return nil;
	}
}

CCAnimation *uiCreateAnimationReverseObject(UIAnimationObject const *Object)
{
	int i;
	CCAnimation *_Ret;
	NSString *_String;
	NSMutableArray *_Array;

	if (0 < Object->path_table.size())
	{
		_Array = [NSMutableArray array];
		for (i = Object->path_table.size() - 1; i >= 0; i--)
		{
			_String = [NSString stringWithUTF8String:Object->path_table[i].c_str()];
			[_Array addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_String]];
		}
		_Ret = [CCAnimation animationWithFrames:_Array delay:Object->delay];
		return _Ret;
	}
	else
	{
		return nil;
	}
}

CCMenuItemImage *uiCreateMenuItemImageObject(UIMenuItemImageObject const *Object, CCNode *Node, SEL Sel)
{
	CCMenuItemImage *_Ret;
	CCSprite *_Image[3];

	for (int i = 0; i < 3; ++i)
	{
		_Image[i] = nil;
	}
	if (0 != strcmp("NULL", Object->path_normal))
	{
		_Image[0] = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:Object->path_normal]];
	}
	if (0 != strcmp("NULL", Object->path_selected))
	{
		_Image[1] = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:Object->path_selected]];
	}
	if (0 != strcmp("NULL", Object->path_disabled))
	{
		_Image[2] = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:Object->path_disabled]];
	}
	_Ret = [CCMenuItemImage itemFromNormalSprite:_Image[0] selectedSprite:_Image[1] disabledSprite:_Image[2] target:Node selector:Sel];
	[_Ret setPositionInPixels:Object->position];
	return _Ret;
}

CCMenuItemImage *uiCreateMenuItemAniObject(UIMenuItemAniObject const *Object, CCNode *Node, SEL Sel)
{
	CCMenuItemImage *_Ret;
	CCSprite *_Image;
	id _Ani;

	_Image = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:Object->path]];
	_Ani = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&Object->ani)];
	[_Image runAction:[CCRepeatForever actionWithAction:_Ani]];
	_Ret = [CCMenuItemImage itemFromNormalSprite:_Image selectedSprite:nil disabledSprite:nil target:Node selector:Sel];
	[_Ret setPositionInPixels:Object->position];
	return _Ret;
}

CCMenu *uiNodeAddMenu(id Node, CGPoint Position, CCMenuItem *Item, ...)
{
	CCMenu *_Ret;
	va_list _Arg;

	va_start(_Arg, Item);
	_Ret = [[CCMenu alloc] initWithItems:Item vaList:_Arg];
	va_end(_Arg);
	[Node addChild:_Ret];
	[_Ret release];
	[_Ret setPositionInPixels:Position];
	return _Ret;
}

CCSprite *uiNodeAddImage(id Node, NSString *Path, int Z, CGPoint Position)
{
	CCSprite *_Ret;

	_Ret = [[CCSprite alloc] initWithFile:Path];
	[Node addChild:_Ret z:Z];
	[_Ret release];
	[_Ret setPositionInPixels:Position];
	return _Ret;
}

bool uiPointInCenterRect(CGPoint Point, UICenterRect Rect)
{
	float _W2;
	float _H2;

	_W2 = Rect.size.width / 2.0f;
	_H2 = Rect.size.height / 2.0f;
	if ((Rect.center.x - _W2 <= Point.x) && (Point.x <= Rect.center.x + _W2) &&
	    (Rect.center.y - _H2 <= Point.y) && (Point.y <= Rect.center.y + _H2))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void uiRenderLine(CGPoint Point1, CGPoint Point2)
{
	GLfloat _VertexArray[] = {Point1.x, Point1.y, Point2.x, Point2.y};
	GLubyte _IndexArray[] = {0, 1};

	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	glVertexPointer(2, GL_FLOAT, 0, _VertexArray);
	glDrawElements(GL_LINES, sizeof _IndexArray, GL_UNSIGNED_BYTE, _IndexArray);

	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
}

void uiRenderTriangle(CGPoint Point1, CGPoint Point2, CGPoint Point3)
{
	GLfloat _VertexArray[] = {Point1.x, Point1.y, Point2.x, Point2.y, Point3.x, Point3.y};
	GLubyte _IndexArray[] = {0, 1, 2};

	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	glVertexPointer(2, GL_FLOAT, 0, _VertexArray);
	glDrawElements(GL_TRIANGLES, sizeof _IndexArray, GL_UNSIGNED_BYTE, _IndexArray);

	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
}

void uiRenderRect(UICenterRect Rect)
{
	float _Top, _Bottom, _Left, _Right;
	GLfloat _VertexArray[8];
	GLubyte _IndexArray[] = {0, 1, 2, 2, 3, 0};

	_Top = Rect.center.y + Rect.size.height / 2;
	_Bottom = Rect.center.y - Rect.size.height / 2;
	_Left = Rect.center.x - Rect.size.width / 2;
	_Right = Rect.center.x + Rect.size.width / 2;

	_VertexArray[0] = _Left; _VertexArray[1] = _Bottom;
	_VertexArray[2] = _Right; _VertexArray[3] = _Bottom;
	_VertexArray[4] = _Right; _VertexArray[5] = _Top;
	_VertexArray[6] = _Left; _VertexArray[7] = _Top;

	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	glVertexPointer(2, GL_FLOAT, 0, _VertexArray);
	glDrawElements(GL_TRIANGLES, sizeof _IndexArray, GL_UNSIGNED_BYTE, _IndexArray);

	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
}